package org.fireflow2.designer.eclipse.modelwrapper;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.AbsoluteBendpoint;
import org.eclipse.draw2d.Bendpoint;
import org.eclipse.draw2d.geometry.Point;
import org.fireflow.model.ModelElement;
import org.fireflow.pdl.fpdl20.diagram.DiagramElement;
import org.fireflow.pdl.fpdl20.diagram.basic.Line;
import org.fireflow.pdl.fpdl20.diagram.basic.impl.PointImpl;

public class ConnectorWrapper extends ModelWrapper {
	SubProcessWrapper subProcessWrapper = null;
	ModelWrapper source = null;
	ModelWrapper target = null;
	List<Bendpoint> constraints = new ArrayList<Bendpoint>();
	
	public ConnectorWrapper(SubProcessWrapper subflowWrapper,ModelElement arg0,DiagramElement diagramElement) {
		super(arg0,diagramElement);
		this.subProcessWrapper = subflowWrapper;
		this.constraints = convertExtendedAttributes2BendpointList();
	}
	public SubProcessWrapper getSubProcessWrapper(){
		return this.subProcessWrapper;
	}

	public List<Bendpoint> getBendpoints() {
		return null;
	}

	public ModelWrapper getSource() {
		return source;
	}

	public void setSource(ModelWrapper source) {
		this.source = source;
	}

	public void setTarget(ModelWrapper target) {
		this.target = target;
	}

	public ModelWrapper getTarget() {
		return target;
	}

	public List<Bendpoint> getConstraints() {
		return constraints;
	}

	public void addConstraint(int index, Bendpoint bendpoint) {
		if (!constraints.contains(bendpoint)) {
			constraints.add(index, bendpoint);
			convertBendpointList2ExtendedAttributes(constraints);
			firePropertyChange(BENDPOINT, null, bendpoint);
		}
	}

	public Bendpoint setConstraint(int index, Bendpoint bendpoint) {
		Bendpoint oldValue = constraints.set(index, bendpoint);
		convertBendpointList2ExtendedAttributes(constraints);
		firePropertyChange(BENDPOINT, null, bendpoint);
		return oldValue;
	}

	public Bendpoint removeConstraint(int index) {
		Bendpoint bendpoint = constraints.get(index);
		Bendpoint removed = constraints.remove(index);
		convertBendpointList2ExtendedAttributes(constraints);
		firePropertyChange(BENDPOINT, bendpoint, null);
		
		return removed;
	}
	

	
	private void calculateNewLabelPosition(){
		Point oldPosition =  this.getLabelPosition();
		if (oldPosition==null) return ;
		
	}
	
	private void convertBendpointList2ExtendedAttributes(List<Bendpoint> constraints){
		Line line = (Line)this.getDiagramElement().getShape();
		line.getPoints().clear();
		for (int i=0;i<constraints.size();i++){
			AbsoluteBendpoint bendpoint = (AbsoluteBendpoint)constraints.get(i);
			PointImpl p = new PointImpl();
			p.setX(bendpoint.x);
			p.setY(bendpoint.y);
			line.getPoints().add(p);
		}
	}
	
	private List<Bendpoint> convertExtendedAttributes2BendpointList(){
		Line line = (Line)this.getDiagramElement().getShape();
		List<Bendpoint> l = new ArrayList<Bendpoint>();
		List<org.fireflow.pdl.fpdl20.diagram.basic.Point> points = line.getPoints();
        for (org.fireflow.pdl.fpdl20.diagram.basic.Point firePoint : points) {
            Bendpoint p = new AbsoluteBendpoint(firePoint.getX(),firePoint.getY());
            l.add(p);
        }

        return l;
	}
	

    
	public void setLabelPosition(Point newLocation){
		Line line = (Line)this.getDiagramElement().getShape();
		org.fireflow.pdl.fpdl20.diagram.basic.Point oldPoint = line.getLabelPosition();
		org.fireflow.pdl.fpdl20.diagram.basic.Point newPoint = new PointImpl(newLocation.x,newLocation.y);
		line.setLabelPosition(newPoint);
		
		this.firePropertyChange(LABEL_POSITION, oldPoint, newPoint);
	}
	
	public Point getLabelPosition(){
		
		Line line = (Line)this.getDiagramElement().getShape();
		if (line.getLabelPosition()!=null){
			return new Point(line.getLabelPosition().getX(),line.getLabelPosition().getY());
		}
		return null;

	}	
	
	public  String getElementType(){
		return "";
	}		
}
